package com.wealth.commodity.web.controller;
import iaas.enums.StatusEnum;
import iaas.utils.ResultObject;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.cart.entity.Cart;
import com.wealth.cart.entity.CartGoodsInfo;
import com.wealth.cart.service.ICartItemService;
import com.wealth.cart.service.ICartService;
import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.service.ICategoryService;
import com.wealth.commodity.service.ICommentService;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.platform.service.IPlatformActivityService;
import com.wealth.system.web.controller.BaseController;



/**
 * 实体商品购物车
 * @author Administrator
 */
@Controller
@RequestMapping(value="/manage/commodityCart")
public class GoodsCartController extends BaseController{
	
	Logger logger = Logger.getLogger(GoodsCartController.class);
	@Autowired 
	private ICategoryService categoryService;
	@Autowired 
	private IGoodsService goodsService;
	@Autowired 
	private ICommentService commentService;
	@Autowired 
	private ICartService cartService;
	@Autowired 
	private ICartItemService cartItemService;
	@Autowired
	private IPlatformActivityService activityService;
	@Autowired
	private IGoodsService igoodsService;
	
	
	

	 /**
	  * 加入购物车
	  * @param model
	  * @param userId 会员id
	  * @param goodsId 商品id
	  * @return
	  */
	@RequestMapping(value = "/addCart")
	public String addCart(Model model,HttpServletRequest request, Long goodsId){
		try {
			 String amount = request.getParameter("amount");
			 if(amount==null || amount==""){
				 amount = "1";
			 }
			 Goods goods = igoodsService.get(goodsId);
			 Long marketId=goods.getMerchant().getMarket().getId();
			 cartService.addCart(memberid,Integer.parseInt(amount), goods,marketId);
			
			return "redirect:/manage/commodityCart/cartShow?marketId="+marketId;
		} catch (Exception e) {
			logger.error("加入购物车错误！！！", e);
			return "system/500";
		}
		
	}
	
	
	/**
	  * 异步加入购物车
	  * @param model
	  * @param userId 会员id
	  * @param goodsId 商品id
	  * @return
	  */
	@RequestMapping(value = "/ajaddCart",method=RequestMethod.POST)
	@ResponseBody
	public ResultObject ajaddCart(Model model, HttpSession session, Long goodsId, int amount){
		try {
			 
			 Goods goods = igoodsService.get(goodsId);
			 Long marketId=goods.getMerchant().getMarket().getId();
			 cartService.addCart(memberid,amount,goods,marketId);
			
			 //购物车 
			 Cart cart=cartService.findCartByMemberIdAndMarketId(memberid, 3L);
			 int count = 0;
			 if(cart!=null){
				 for (int i = 0; i < cart.getCartItems().size(); i++) {
					 count = count + cart.getCartItems().get(i).getAmount();
				}
			 }
			 return new ResultObject("000", "加入成功", count);
		} catch (Exception e) {
			logger.error("加入购物车错误！！！", e);
			 
		}
		return null;
	}
	
	
	
	
	
	/**
	 * 购物车展示
	 * @param request
	 * @param model
	 * @param marketId
	 * @return
	 */
	@RequestMapping(value = "/cartShow",method=RequestMethod.GET)
	public String cartShow(HttpServletRequest request, Model model, Long marketId) {
		try {

			List<CartGoodsInfo> cartGoodsList  = cartService.getCartGoodsByMemberId(memberid,marketId);
			model.addAttribute("cartGoodsList", cartGoodsList);
			//获取平台活动
			if(activityService.findByStatusAndType(StatusEnum.NORMAL.getValue(),0).size()>0){
				model.addAttribute("activity", activityService.findByStatusAndType(StatusEnum.NORMAL.getValue(),0).get(0));
			}
			//购物车总价
			Cart cart=cartService.findCartByMemeberIdAndMarketId(memberid, marketId);
			double total = 0d;
			if(cart!=null){
				total=cartService.getTotal(cart);
			}
			
			model.addAttribute("total", total);
			model.addAttribute("marketId", marketId);
			return "commodity/cart";
			
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	
	/**
	 * 购物车详情查询
	 * @param request
	 * @param model
	 * @param goodsId
	 * @param amount
	 * @return
	 */
	@RequestMapping(value = "/cartItemShow",method=RequestMethod.POST)
	@ResponseBody
	public ResultObject ajxaCartItem(HttpServletRequest request, Model model, Long marketId) {
		
		try {
			
			List<CartGoodsInfo> cartGoodsList  = cartService.getCartGoodsByMemberId(memberid,marketId);
			return new ResultObject("000", "查询成功", cartGoodsList);
			
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	
	

	
	/**
	 * 修改购物车
	 * @param request
	 * @param model
	 * @param goodsId
	 * @param amount
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public ResultObject saveOrUpdate(HttpServletRequest request, Model model,Long goodsId,int amount,Long marketId) {
		
		try {
			Map<String, Object> map=cartService.saveOrUpdate(goodsId, amount,memberid,marketId);
			
			return new ResultObject("000", "查询成功", map);
			
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	
	
	/**
	 * 选中购物车的商品
	 * @param request
	 * @param model
	 * @param goodsId
	 * @return
	 */
	@RequestMapping(value = "/checkCartItem")
	@ResponseBody
	public ResultObject checkCartItem(HttpServletRequest request, Model model,Long goodsId,Integer status,Long marketId,int amount) {
		
		try {
			Map<String, Object> map=cartService.checkCartItem(goodsId,status,memberid,marketId,amount);
			return new ResultObject("000", "查询成功", map);
			
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	
	/**
	 * 选中购物车
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/checkCart")
	@ResponseBody
	public ResultObject checkCart(HttpServletRequest request, Model model,Integer status,Long marketId) {
		
		try {
			Map<String, Object> map =cartService.checkCart(status,memberid,marketId);
			return new ResultObject("000", "查询成功", map);
			
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	

	/**
	 * 删除购物车明细
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/delCartItem",method=RequestMethod.POST)
	@ResponseBody
	public ResultObject deleteCartItem(HttpServletRequest request, Model model,Long goodsId,Long marketId) {
		
		try {
			
			Map<String, Object> map=cartService.delCartItem(goodsId, memberid, marketId);
			return new ResultObject("000", "删除成功", map);
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
}
